package com.yunxi.service.localService.sys.impl;

import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.core.web.TreeConfig;
import com.yunxi.dao.sys.MenuMapper;
import com.yunxi.model.sys.Menu;
import com.yunxi.service.localService.sys.MenuService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by guan on 2018/9/29.
 */
@Service
public class MenuServiceImpl extends ServiceImplPlus<MenuMapper,Menu> implements MenuService {

    @Cacheable(value="menus",key = "'menu:data_'+#sys_type")
    public Object findTreeListBySysType(String sys_type) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Map map = new HashMap();
        map.put("sys_type",sys_type);

        List list = baseMapper.queryByList(map);
        if(sys_type!=null&&sys_type.equalsIgnoreCase("admin")){
            TreeConfig config = new TreeConfig("0", "菜单", true, "menuId", "menuName", "pid", "hasChild");
            return config.toTreeData(list,false);
        }else{
            TreeConfig config = new TreeConfig("1", "菜单", true, "menuId", "menuName", "pid", "hasChild");
            return config.toTreeData(list,false);
        }
    }

    @Cacheable(value="menus",key = "'menu:all'")
    public Object findTreeList() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Map map = new HashMap();
        List list = baseMapper.queryByList(map);
        TreeConfig config = new TreeConfig("-1", "菜单", true, "menuId", "menuName", "pid", "hasChild");
        return config.toTreeData(list,false);
    }

    @Override
    public List getMenu() {
        return baseMapper.getMenu();
    }

    @CacheEvict(value="menus", allEntries=true)
    public int insert(Menu var1){
        return baseMapper.insert(var1);
    }
}
